﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace CommonUtils
{
    public enum LogLevels
    {
        /// <summary>
        /// Info
        /// </summary>
        I = 0,
        /// <summary>
        /// Warn
        /// </summary>
        W = 1,
        /// <summary>
        /// Error
        /// </summary>
        E = 2,
        /// <summary>
        /// Fatal
        /// </summary>
        F = 3
    }

    public enum LogOutputTypes
    {
        /// <summary>
        /// don't output to debugview and log file
        /// </summary>
        NoLogOutput = 0,
        /// <summary>
        /// only debug view
        /// </summary>
        DebugView = 1,
        /// <summary>
        /// log file and debugview
        /// </summary>
        FileAndDebugView = 2
    }

    /// <summary>
    /// configure log path, log level, log ouput type
    /// </summary>
    public static class LogConfigure
    {
        //output log level
        static string _logFolder;
        static bool _isOutputToFile = false;
        static LogLevels _logLevel = LogLevels.I;

        public static void SetLogFolder(string logFolder)
        {
            if (string.IsNullOrEmpty(logFolder)) return;

            //path has invalid char
            var pathCharArray = logFolder.ToCharArray();
            if (pathCharArray.Any(o => Path.GetInvalidPathChars().Contains(o)))
                return;

            //EnvironmentVariables Path
            if (logFolder.Contains('%'))
                logFolder = Environment.ExpandEnvironmentVariables(logFolder);



            //risk:directory readonly;need administrator right to createfile;and so on
            //use try-catch
            try
            {
                FileFolderHelper.CreateFolder(logFolder);
                _logFolder = logFolder;
            }
            catch (Exception ex)
            {
                Trace.Write(ex);
            }
        }
        /// <summary>
        /// init by configuration
        /// </summary>
        public static void Init(string logFolder, LogOutputTypes logType, LogLevels loglevel)
        {
            _logLevel = loglevel;
 
            if(_logLevel == LogLevels.I)
            {
                Log4netWrapper.MaxLogFiles = 5000;
            }
            else
            {
                Log4netWrapper.MaxLogFiles = 10;
            }
            #region switch log type
            Trace.AutoFlush = true;

            switch (logType)
            {
                case LogOutputTypes.NoLogOutput:
                    {
                        //0 - No output
                        Trace.Listeners.Clear();
                        return;
                    }
                case LogOutputTypes.DebugView:
                    {
                        //1 - Debugger (Such as DbgView)
                        //use default listener
                        return;
                    }
                case LogOutputTypes.FileAndDebugView:
                    {
                        _isOutputToFile = true;
                        break;
                    }

                default: return;
            }
            #endregion
        }
    

        #region public property
        public static string LogFolder
        {
            get { return _logFolder; }
        }

        public static LogLevels LogLevel
        {
            get { return _logLevel; }
        }

        public static bool IsOutputToFile
        {
            get { return _isOutputToFile; }
        }
        #endregion
    }
}